/**
 * Copyright (C) 2010 DustedPixels.com
 */
package com.dustedpixels.cirt.model.cpu.z80;

import com.dustedpixels.cirt.model.core.Circuit;
import com.dustedpixels.cirt.model.core.In;
import com.dustedpixels.cirt.model.core.Internal;
import com.dustedpixels.cirt.model.core.Out;
import com.dustedpixels.cirt.model.core.Pins;
import com.dustedpixels.cirt.model.core.Script;

/**
 * 8-bit parity calculator.
 *
 * @author Michal Pociecha-Los (micapolos@gmail.com)
 */
@Circuit
public final class Parity8 {
  @In @Pins(8) public int in;
  @Out public boolean outParity;
  
  @Internal public boolean[] parityArray;
  
  public Parity8() {
    parityArray = new boolean[1 << 8];
    for (int i = 0; i < parityArray.length; i++) {
      parityArray[i] = (Integer.bitCount(i) & 1) == 1;
    }
  }
  
  @Script
  public void update() {
    outParity = parityArray[in];
  }
}
